/*************************************************************************
	> File Name: P1507.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-04-02 16:07:02
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

int dp[500][500];

int main() {
    int H, T, n;
    cin >> H >> T >> n;
    dp[0][0] = 1;
    for (int m = 0, h, t, k; m < n; m++) {
        cin >> h >> t >> k;
        for (int i = H; i >= h; i--) {
            for (int j = T; j >= t; j--) {
                dp[i][j] = max(dp[i][j], dp[i - h][j - t] + k);
            }
        }
    }
    cout << dp[H][T] << endl;
    return 0;
}
